library('sf')
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library('tmap')
library('tmaptools')
library('tidyverse')
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library('here')
## here() starts at C:/Users/diana/CASA/Term1/0005_GIS/Practical/week5/wk5exam2/w4pratical
library(countrycode)
## Warning: package 'countrycode' was built under R version 4.2.2
#install.packages("countrycode")
#package country code installed, hence removed it from the code segment as it
#repeatedly prompted restart while attempting to knit on save.
world_shape <- st_read(here("World_Countries_(Generalized)","World_Countries__Generalized_.shp"))
## Reading layer `World_Countries__Generalized_' from data source
## `C:\Users\diana\CASA\Term1\0005_GIS\Practical\week5\wk5exam2\w4pratical\World_Countries_(Generalized)\World_Countries__Generalized_.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 251 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS: WGS 84
gdi <- read_csv(here("gdi.csv"))
## New names:
## Rows: 206 Columns: 7
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (4): iso3, country, hdicode, region dbl (2): gdi_2010, gdi_2019 lgl (1): ...7
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...7`
gdi <- gdi %>%
mutate(diff=gdi_2019-gdi_2010)
#gdi <- gdi %>%
# rename(ISO=iso3)
#countrycode('Albania', 'country.name', 'iso3c')
gdi1 <- gdi %>%
mutate(code= countrycode(country,'country.name', 'iso3c'))
## Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: Arab States, East Asia and the Pacific, Europe and Central Asia, High human development, Latin America and the Caribbean, Low human development, Medium human development, South Asia, Sub-Saharan Africa, Very high human development, World
world_shape1 <- world_shape %>%
mutate(code= countrycode(COUNTRY,'country.name', 'iso3c'))
## Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: Azores, Bonaire, Canarias, Glorioso Islands, Juan De Nova Island, Madeira, Micronesia, Saba, Saint Eustatius, Saint Martin, Turkiye
shape <- left_join(world_shape1, gdi1, by = "code")
#plot
library(tmap)
tmap_mode("plot")
## tmap mode set to plotting
# change the fill to your column name if different
shape %>%
qtm(.,fill = "diff")
## Variable(s) "diff" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
print(shape)
## Simple feature collection with 361 features and 16 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS: WGS 84
## First 10 features:
## FID COUNTRY ISO COUNTRYAFF AFF_ISO SHAPE_Leng
## 1 1 Afghanistan AF Afghanistan AF 50.8032097
## 2 2 Albania AL Albania AL 9.6250380
## 3 3 Algeria DZ Algeria DZ 70.5400822
## 4 4 American Samoa AS United States US 0.6001244
## 5 5 Andorra AD Andorra AD 0.9373518
## 6 6 Angola AO Angola AO 58.8734698
## 7 7 Anguilla AI United Kingdom GB 0.4683887
## 8 8 Antarctica AQ <NA> <NA> 1564.1441426
## 9 9 Antigua and Barbuda AG Antigua and Barbuda AG 1.1629014
## 10 10 Argentina AR Argentina AR 138.7513483
## SHAPE_Area code iso3 country hdicode region gdi_2010
## 1 6.256671e+01 AFG AFG Afghanistan Low SA 0.627
## 2 3.073540e+00 ALB ALB Albania High ECA 0.960
## 3 2.135135e+02 DZA DZA Algeria High AS 0.838
## 4 1.371972e-02 ASM <NA> <NA> <NA> <NA> NA
## 5 5.558517e-02 AND AND Andorra Very High <NA> NA
## 6 1.038131e+02 AGO AGO Angola Medium SSA 0.867
## 7 7.797911e-03 AIA <NA> <NA> <NA> <NA> NA
## 8 5.672928e+03 ATA <NA> <NA> <NA> <NA> NA
## 9 4.562595e-02 ATG ATG Antigua and Barbuda High LAC NA
## 10 2.783090e+02 ARG ARG Argentina Very High LAC 0.995
## gdi_2019 ...7 diff geometry
## 1 0.712 NA 0.085 MULTIPOLYGON (((61.27655 35...
## 2 1.002 NA 0.042 MULTIPOLYGON (((19.57083 41...
## 3 0.880 NA 0.042 MULTIPOLYGON (((4.603354 36...
## 4 NA NA NA MULTIPOLYGON (((-170.7439 -...
## 5 NA NA NA MULTIPOLYGON (((1.445836 42...
## 6 0.902 NA 0.035 MULTIPOLYGON (((23.47611 -1...
## 7 NA NA NA MULTIPOLYGON (((-63.16778 1...
## 8 NA NA NA MULTIPOLYGON (((-180 -84.30...
## 9 NA NA NA MULTIPOLYGON (((-61.73806 1...
## 10 1.000 NA 0.005 MULTIPOLYGON (((-71.85916 -...
tmap_mode("plot")
## tmap mode set to plotting
breaks=c(-0.6,-0.03,-0.01, 0.01, 1, 50, 100, 150, 200)
tm1 <- tm_shape(shape) +
tm_polygons("gdi_2010",
breaks=breaks,
palette="PuOr")+
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(a1: gii_2019)", position=c(0,0.85), size=1.5)
tm2 <- tm_shape(shape) +
tm_polygons("gdi_2019",
breaks=breaks,
palette="PuOr")+
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(a2: gii_2010)", position=c(0,0.85), size=1.5)
tm3 <- tm_shape(shape) +
tm_polygons("diff",
breaks=breaks,
palette="PuOr")+
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(a3: gii_rank_2021)", position=c(0,0.85), size=1.5)
legend <- tm_shape(shape) +
tm_polygons("diff",
breaks=breaks,midpoint = NA,
palette="PuOr") +
tm_scale_bar(position=c(-0.4,0.8), text.size=0.6)+
tm_compass(north=0, position=c(0.65,0.6))+
tm_layout(legend.only = TRUE, legend.position=c(0.2,0.25),asp=0.1)+
tm_credits("Gender Inequality Difference 2010 & 2019", position=c(0.0,0.0))
t=tmap_arrange(tm1, tm2, tm3, legend, ncol=2)
print(t)
## Variable(s) "diff" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
## Scale bar set for latitude km and will be different at the top and bottom of the map.
tmap_mode("view")
## tmap mode set to interactive viewing
breaks=c(-0.6,-0.03,-0.01, 0.01, 1)
tm_shape(shape) +
tm_polygons("diff", breaks=breaks, midpoint = NA)
```